package com.sokal.easycare.modules.product.service.product;

import java.util.List;

import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sokal.easycare.common.persistence.Page;
import com.sokal.easycare.common.service.BaseService;
import com.sokal.easycare.common.service.CrudService;
import com.sokal.easycare.common.utils.StringUtils;
import com.sokal.easycare.common.utils.QRcoder.QRcoderUtil;
import com.sokal.easycare.modules.product.dao.product.ProAttributeDao;
import com.sokal.easycare.modules.product.dao.product.ProProductDao;
import com.sokal.easycare.modules.product.entity.product.ProAttribute;
import com.sokal.easycare.modules.product.entity.product.ProProduct;
import com.sokal.easycare.modules.sys.entity.MainUser;
import com.sokal.easycare.modules.sys.utils.UserUtils;

/**
 * 产品Service
 * 
 * @author liliangdong
 * @version 2017-03-12
 */
@Service
@Transactional(readOnly = true)
public class ProProductService extends CrudService<ProProductDao, ProProduct> {

	@Autowired
	private ProAttributeDao proAttributeDao;

	@Autowired
	private ProProductDao ProProductDao;

	public ProProduct get(String id) {
		ProProduct proProduct = super.get(id);
		proProduct.setProAttributeList(proAttributeDao.findList(new ProAttribute(proProduct)));
		return proProduct;
	}

	public List<ProProduct> findList(ProProduct proProduct) {
		return super.findList(proProduct);
	}

	public Page<ProProduct> findPage(Page<ProProduct> page, ProProduct proProduct) {
		MainUser user = UserUtils.getUser();
		proProduct.getSqlMap().put("dsf", BaseService.dataScopeFilter(user.getCurrentUser(), "o", "u"));
		return super.findPage(page, proProduct);
	}

	@Transactional(readOnly = false)
	public void save(ProProduct proProduct) {
		proProduct.setContent(StringEscapeUtils.unescapeHtml4(proProduct.getContent()));
		super.save(proProduct);
		for (ProAttribute proAttribute : proProduct.getProAttributeList()) {
			if (proAttribute.getId() == null) {
				continue;
			}
			if (ProAttribute.DEL_FLAG_NORMAL.equals(proAttribute.getDelFlag())) {
				if (StringUtils.isBlank(proAttribute.getId())) {
					proAttribute.setProProduct(proProduct);
					proAttribute.preInsert();
					proAttributeDao.insert(proAttribute);
				} else {
					proAttribute.preUpdate();
					proAttributeDao.update(proAttribute);
				}
			} else {
				proAttributeDao.delete(proAttribute);
			}
		}
	}

	@Transactional(readOnly = false)
	public void delete(ProProduct proProduct) {
		super.delete(proProduct);
		proAttributeDao.delete(new ProAttribute(proProduct));
	}

	@Transactional(readOnly = false)
	public void createTwoDimlCode(String productId, String codePath) throws Exception {
		ProProduct proProduct = super.get(productId);
		proProduct.setTwoDimlCode(QRcoderUtil.encode(productId, codePath, true));
		ProProductDao.updateTwoDimlCode(proProduct);
	}

}